package com.google.gwt.sample.hello.client.notgenerated.eventFramework;

import java.util.ArrayList;
import java.util.List;

/**
 * A utility class to make the registration and removal of property listeners,
 * and the firing of property events, easier. 
 * 
 * @param <T> parameterized type of Property
 */
public class PropertyChangeSupport<T>
{
   private Property<T> source;
   private List<PropertyChangeListener<T>> globalListeners = new ArrayList<PropertyChangeListener<T>>();

   public PropertyChangeSupport(Property<T> source)
   {
      this.source = source;
   }

   public void addPropertyChangeListener(PropertyChangeListener<T> listener)
   {
      globalListeners.add(listener);
   }

   public void removePropertyChangeListener(PropertyChangeListener<T> listener)
   {
      globalListeners.remove(listener);
   }

   public void firePropertyChange(T oldValue, T newValue)
   {
      for (PropertyChangeListener<T> listener : globalListeners)
      {
         listener.propertyChanged(new PropertyChangeEvent<T>(source, oldValue, newValue));
      }
   }
}

